'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceFJxfpCtrl
 * @description
 * # FinanceFJxfpCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('FinanceFJxfp1Ctrl', function ($scope, localStorageService, $rootScope, openApi, Util, $location, $routeParams, FormSubmit) {
        var id = $scope.process_id = $routeParams.id
        //  增值税普通发票,增值税专用发票,普通商业发票,其它
        $scope.payTypeList = [{
            key: "增值税普通发票",
            value: "增值税普通发票"
        }, {
            key: "增值税专用发票",
            value: "增值税专用发票"
        }, {
            key: "普通商业发票",
            value: "普通商业发票"
        }, {
            key: "其它",
            value: "其它"
        }];

        $scope.electronicList = [{
            key: "是",
            value: 1
        }, {
            key: "否",
            value: 0
        }];
        // 3%,6%,10%,13%,16%,其它
        $scope.rateList = [{
            key: "无",
            value: "无"
        }, {
            key: "1%",
            value: "1%"
        }, {
            key: "2%",
            value: "2%"
        }, {
            key: "3%",
            value: "3%"
        }, {
            key: "5%",
            value: "5%"
        }, {
            key: "6%",
            value: "6%"
        }, {
            key: "9%",
            value: "9%"
        }, {
            key: "10%",
            value: "10%"
        }, {
            key: "11%",
            value: "11%"
        }, {
            key: "13%",
            value: "13%"
        }, {
            key: "16%",
            value: "16%"
        }, {
            key: "17%",
            value: "17%"
        }, {
            key: "免税",
            value: "免税"
        }, {
            key: "其他",
            value: "其他"
        }];

        
        $scope.chooseProject = function () {
            // 将列表的数据序列化存入缓存以便回来时候使用
            Util.savaParamForReview($scope.param, id);
            $location.path("project/chooseproject");
        };
        $scope.chooseContract = function () {
            var params = $scope.param;
            if (!params.projectId || !params.contractType) {
                Util.Toast("请先选择项目与合同类型");
                return;
            }
            var projectParams = {
                formKey: params.contractType,
                fieldName: "contractNo",
                projectId: params.projectId,
                projectNo: params.projectNo
            };

            // 清掉关联的编号
            Util.savaParamForReview(params, id);
            localStorageService.set("projectParams", projectParams);
            $location.path("/project/choosedata");
        };

        $scope.chooseSupplier = function () {
            var params = $scope.param;
            var projectParams = {
                formKey: "Xm_SupplierStore",
                fieldName: "supplierNo"
            };

            // 清掉关联的编号
            Util.savaParamForReview(params, id);
            localStorageService.set("projectParams", projectParams);
            $location.path("/project/choosedata");
        };
        $scope.showCertificate = function () {
            $scope.showCerPicker = true;
            $rootScope.$emit("showmasker");
        };
        $scope.deletedItem = function (index) {
            $scope.param.details.splice(index, 1);
            $scope.costindex = -1;
        };

        $scope.addCertificate = function () {
            $scope.picker = $scope.picker || {};
            var check = false;
            check = checkPicker($scope.picker);
            if (!check) {
                return;
            }
            $scope.param.details.push($scope.picker);
            // 隐藏表单
            closePicker();
        };
        var closePicker = function () {
            $scope.picker = {};
            $rootScope.$emit("closemasker");
            $scope.showCerPicker = false;
        };
        $scope.$on("closemasker", function () {
            closePicker();
        });
        $scope.closePicker = function () {
            closePicker();
        };
        $scope.swipe = function (index) {
            $scope.costindex = index;
        };
        $scope.resetlist = function () {
            $scope.costindex = -1;
        };
        // 验证是否输入完整
        var checkPicker = function (data) {
            if (!data.invoiceNo) {
                Util.Toast("请填写发票编号");
                return;
            }
            if (!data.openDate) {
                Util.Toast("请选择开票日期");
                return;
            }
            if (!data.invoiceAmount) {
                Util.Toast("请填写发票金额(不含税)");
                return;
            }
            if (!data.invoiceTaxAmount) {
                Util.Toast("请填写发票税额");
                return;
            }
            if (typeof data.signIn == "undefined" || data.signIn == "") {
                Util.Toast("请选择签收状态");
                return;
            }
            if (typeof data.taxDeclare == "undefined" || data.taxDeclare == "") {
                Util.Toast("请选择报税状态");
                return;
            }
            return true;
        };

        $scope.submit = function (operationId) {
            // 底部类型end
            var options = {};
            // 流程id
            options.id = id;
            // 根据operationId判断是草稿还是提交
            options.operationId = operationId;
            options.urlParam = openApi.addinputInvoice;
            options.param = angular.copy($scope.param, {});
            options.param.invoiceDetail = JSON.stringify(options.param.details);
            options.param.settlementType = 1

            delete options.param.details
            FormSubmit.submit(options, $scope);
        };
        $scope.validate = function () {
            if (!$scope.param.projectName) {
                Util.Toast("请选择项目名称");
                return;
            }

            if (!$scope.param.makeInvoiceCompany) {
                Util.Toast("请选填写开票单位");
                return;
            }
            if (!$scope.param.invoiceType) {
                Util.Toast("请选择发票类型");
                return;
            }

            if (!$scope.param.taxRate) {
                Util.Toast("请选择税率");
                return;
            }

            if (typeof $scope.param.electronic === "undefined") {
                Util.Toast("请选择是否是电子发票");
                return;
            }

            if (!$scope.param.details) {
                Util.Toast("请填写发票详情");
                return;
            }

            if (!$scope.validation.passvalidation) {
                Util.Toast($scope.validation.tip);
                return;
            }
            return true;
        };
        var init = function () {
            // 获取回显的详细信息
            Util.getDetailData(id, function (data) {
                $scope.param = data;
                var data = localStorageService.get("projectinfo");
                localStorageService.remove("projectinfo");
                // 获取项目关联数据
                var projectreldata = localStorageService.get("projectreldata");
                localStorageService.remove("projectreldata");
                $scope.param = Object.assign({}, $scope.param, data, projectreldata);
                $scope.param.details = $scope.param.details || []
            });
        };
        // 初始化
        init();
    });
